import { defineStore } from 'pinia'
import { logout } from '@/api/user'
import { ref } from 'vue';

export const useUserStore = defineStore('user', () => {
  const token = ref("")
  const userInfo = ref({} as UserInfo)
  function SET_TOKEN(name: string) {
    token.value = name
    localStorage.setItem("token", name)
  }
  function SET_INFO(user: UserInfo) {
    userInfo.value = user
  }
  async function remove() {
    await logout()
    localStorage.clear()
    sessionStorage.clear()
    SET_INFO({} as UserInfo)
  }
  
  return {
    persist: true,
    token,
    userInfo,
    remove,
    SET_TOKEN,
    SET_INFO
  }
})
